home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wil4c10.zip / FINGER.C < prev    next >
C/C++ Source or Header  |  1997-07-26  |  8KB  |  319 lines

  1. /*
  2. **  FINGER.C
  3. **
  4. **  FINGER client, uses ASYNC functions.
  5. **
  6. **  Compare to
  7. **    (1) AFINGR -- also async, but without using ASYNC.C functions.
  8. **    (2) SFINGR -- synchronous version.
  9. */
  10.  
  11. #include <windows.h>
  12. #include <winsock.h>
  13.  
  14. #include "wil.h"
  15. #include "message.h"
  16. #include "paint.h"
  17. #include "about.h"
  18. #include "async.h"
  19. #include "str.h"
  20.  
  21.  
  22. #ifdef WIN32
  23. #define USE_INS HINSTANCE
  24. #define USE_PTR PSTR
  25. #else
  26. #define USE_INS HANDLE
  27. #define USE_PTR LPSTR
  28. #endif
  29.  
  30. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  31.  
  32. /* globals */
  33.  
  34. HWND hMainWnd;            /* main window handle */
  35.  
  36. #define BS            8
  37. #define LF           10
  38. #define CR           13
  39. #define MAX_BUF     128
  40. #define MAX_STR      40
  41.  
  42. #define FINGER_PORT  79
  43.  
  44. #define USER_CONN_SUCCESS   100
  45. #define USER_WRITE_SUCCESS  101
  46. #define USER_FAILURE        102
  47. #define USER_SUCCESS        103
  48.  
  49. static HMENU hMenu;
  50. static USE_INS hInstance;
  51. static int WinWidth = 8 * NCOLS;
  52. static int WinHeight = 12 * NROWS + 48;
  53. static char Temp[MAX_BUF+8];
  54. static int  InBufLen = 0;
  55. static char InBuffer[MAX_BUF+1];
  56. static char User[MAX_STR] = "";
  57. static char Host[MAX_STR] = "";
  58. static SOCKET Socket = 0;
  59. static LPSTR  Ptr;
  60. static HCURSOR ArrowCursor;
  61. static HCURSOR WaitCursor;
  62.  
  63. /* add character to buffer */
  64.  
  65. static void Add2Buffer(char Chr)
  66. {/* add char to input buffer */
  67.  switch(Chr)
  68.    {case BS:
  69.       if(InBufLen>0)
  70.         {/* backup on screen */
  71.          DisplayChar(BS);
  72.          /* remove last char from buffer */
  73.          InBufLen--;
  74.         }
  75.       break;
  76.     default:
  77.       /* display char */
  78.       DisplayChar(Chr);
  79.       /* put into buffer */
  80.       if(InBufLen<MAX_BUF) InBuffer[InBufLen++] = Chr;
  81.       break;
  82.    }
  83. }
  84.  
  85. /* display error message */
  86.  
  87. static void DisplayError(int Code, LPSTR Msg)
  88. {DisplayString("ERROR: ");
  89.  if(Msg) DisplayString(Msg);
  90.  if(Code)
  91.    {wilErrorText(Code,(LPSTR)Temp,50);
  92.     DisplayLine((LPSTR)Temp);
  93.    }
  94.  SetCursor(ArrowCursor);
  95. }
  96.  
  97. /* WinMain */
  98.  
  99. #ifdef WIN32
  100. int WINAPI
  101. #else
  102. int PASCAL
  103. #endif
  104. WinMain(USE_INS hInst, USE_INS hPrevInstance,
  105.         USE_PTR szCmdLine,  int nCmdShow)
  106. {WNDCLASS  wc;
  107.  MSG msg;
  108.  BOOL Result;
  109.  if(!hPrevInstance)
  110.    {/* register main window class */
  111.     wc.style = CS_HREDRAW | CS_VREDRAW;
  112.     wc.lpfnWndProc = MainWndProc;
  113.     wc.cbClsExtra = 0;
  114.     wc.cbWndExtra = 0;
  115.     wc.hInstance = hInst;
  116.     wc.hIcon = LoadIcon(hInst, "HostIcon");
  117.     wc.hCursor = NULL;
  118.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  119.     wc.lpszMenuName =  "HostMenu";
  120.     wc.lpszClassName = "HostWClass";
  121.     Result = RegisterClass(&wc);
  122.     if(!Result) return FALSE;
  123.    }
  124.  
  125.  /* create main window */
  126.  hInstance = hInst;
  127.  hMainWnd = CreateWindow(
  128.         "HostWClass",   "FINGER",    WS_OVERLAPPEDWINDOW,
  129.         CW_USEDEFAULT,  CW_USEDEFAULT,
  130.         WinWidth,       WinHeight,
  131.         NULL,           NULL,
  132.         hInstance,      NULL);
  133.  ShowWindow(hMainWnd, nCmdShow);
  134.  UpdateWindow(hMainWnd);
  135.  hMenu = GetMenu(hMainWnd);
  136.  
  137.  /* window control loop */
  138.  
  139.  while(GetMessage(&msg,NULL,0,0))
  140.    {
  141.     TranslateMessage(&msg);
  142.     DispatchMessage(&msg);
  143.    }
  144.  return (msg.wParam);
  145. } /* end WinMain */
  146.  
  147. #ifdef WIN32
  148. LRESULT CALLBACK
  149. #else
  150. long FAR PASCAL
  151. #endif
  152. MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
  153. {int Code;
  154.  HDC hDC;
  155.  PAINTSTRUCT ps;
  156. #ifdef WIN32
  157. #else
  158.  static FARPROC lpfnAboutDlgProc;
  159. #endif
  160.  hMainWnd = hWindow;
  161.  switch (iMsg)
  162.     {case WM_CREATE:
  163.       /* create cursors */
  164.       ArrowCursor = LoadCursor(NULL, IDC_ARROW);
  165.       WaitCursor = LoadCursor(NULL, IDC_WAIT);
  166.       SetCursor(ArrowCursor);
  167. #ifdef WIN32
  168. #else
  169.        /* create thunk for Win16 */
  170.        lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,hInstance);
  171. #endif
  172.       /* initialize paint module */
  173.       PaintInit();
  174.       /* attach WINSOCK */
  175.       DisplayString("Attaching WINSOCK...");
  176.       Code = wilAttach();
  177.       DisplayLine("OK");
  178.       if(Code<0) DisplayError(Code,"wilAttach fails:");
  179.       else
  180.         {wsprintf((LPSTR)Temp," Description: %s", wilGetDescription() );
  181.          DisplayLine((LPSTR)Temp);
  182.          wsprintf((LPSTR)Temp," My HostName: %s", wilGetMyHostName() );
  183.          DisplayLine((LPSTR)Temp);
  184.          wsprintf((LPSTR)Temp," My HostAddr: %s", wilGetMyHostDotted(0) );
  185.          DisplayLine((LPSTR)Temp);
  186.         }
  187.       break;
  188.  
  189.      case WM_COMMAND:
  190.          switch(wParam)
  191.            {case MSG_ABOUT :
  192. #ifdef WIN32
  193.                DialogBox(hInstance, "AboutBox", hMainWnd, AboutDlgProc);
  194. #else
  195.                DialogBox(hInstance, "AboutBox", hMainWnd, lpfnAboutDlgProc);
  196. #endif
  197.                return 0;
  198.  
  199.             case MSG_EXIT:
  200.               wilRelease();
  201.               DestroyWindow(hMainWnd);
  202.               break;
  203.  
  204.             case MSG_DEBUG:
  205.               Code = wilDebug(0);
  206.               wsprintf((LPSTR)Temp,"Debug(0) returned %d",Code);
  207.               DisplayLine((LPSTR)Temp);
  208.               break;
  209.  
  210.             case MSG_BREAK:
  211.               wilCancelBlocking();
  212.               wilAwaitCancel(Socket,hMainWnd);
  213.               break;
  214.  
  215.             case MSG_FINGER:
  216.               InBufLen = 0;
  217.               DisplayString("Enter user (usr@domain):");
  218.               break;
  219.            }
  220.          break;
  221.  
  222.     case WM_PAINT:
  223.       HideCaret(hMainWnd);
  224.       hDC = BeginPaint(hMainWnd, &ps);
  225.       SetMapMode(hDC,MM_ANISOTROPIC);
  226.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  227.       PaintMain(hDC,&ps);
  228.       EndPaint(hMainWnd,&ps);
  229.       SetCaretPos(PaintGetColPos(),PaintGetRowPos());
  230.       ShowCaret(hMainWnd);
  231.       break;
  232.  
  233.     case WM_DESTROY:
  234.       PostQuitMessage(0);
  235.       break;
  236.  
  237.     case WM_USER: /* posted by WIL */
  238.  
  239. #if 0
  240. wsprintf((LPSTR)Temp,"[%ld]", lParam);
  241. DisplayString((LPSTR)Temp);
  242. #endif
  243.  
  244.       AsyncProcessMsg(lParam);
  245.       break;
  246.  
  247.     case WM_USER+1:  /* posted by Async functions */
  248.  
  249. #if 0
  250. wsprintf((LPSTR)Temp,"<%d>", wParam);
  251. DisplayString((LPSTR)Temp);
  252. #endif
  253.  
  254.       switch(wParam)
  255.         {
  256.          case USER_CONN_SUCCESS:
  257.            /* we are now connected. send user name to server */
  258.            wsprintf((LPSTR)Temp,"%s\r\n",(LPSTR)User);
  259.            AsyncWrite((LPSTR)Temp, USER_WRITE_SUCCESS, USER_FAILURE);
  260.            break;
  261.  
  262.          case USER_WRITE_SUCCESS:
  263.            /* now get response text from server */
  264.            AsyncRead(USER_SUCCESS, USER_FAILURE, ASYNC_MULTIPLE_LINES);
  265.            break;
  266.  
  267.          case USER_FAILURE:
  268.            DisplayLine("Finger has failed.");
  269.            /* fall thru... */
  270.  
  271.          case USER_SUCCESS:
  272.            DisplayLine("\r\nFinger completed.");
  273.            SetCursor(ArrowCursor);
  274.            wilCloseSocket(Socket);
  275.            break;
  276.         }
  277.       break;
  278.  
  279.     case WM_CHAR:
  280.       if(wParam==CR)
  281.         {/* do the CR */
  282.          DisplayChar((char)wParam);
  283.          /* user has completed input */
  284.          DisplayChar(LF);
  285.          InBuffer[InBufLen] = '\0';
  286.          /* execute command */
  287.          wsprintf((LPSTR)Temp,"User@Host: %s",(LPSTR)InBuffer);
  288.          DisplayLine((LPSTR)Temp);
  289.          /* extract user & host names */
  290.          Ptr = StringChar((LPSTR)InBuffer,'@');
  291.          if(Ptr==NULL)
  292.            {DisplayError(Code, "Cannot recognize User@Host");
  293.             break;
  294.            }
  295.          *Ptr = '\0';
  296.          lstrcpy((LPSTR)User, (LPSTR)InBuffer);
  297.          lstrcpy((LPSTR)Host, (LPSTR)(++Ptr));
  298.          wsprintf((LPSTR)Temp,"User ='%s'", (LPSTR)User);
  299.          DisplayLine(Temp);
  300.          wsprintf((LPSTR)Temp,"Host ='%s'", (LPSTR)Host);
  301.          DisplayLine(Temp);
  302.          SetCursor(WaitCursor);
  303.          /* connect to server */
  304.          Socket = AsyncConnect(hMainWnd,"Finger",(LPSTR)Host,
  305.                           FINGER_PORT,  USER_CONN_SUCCESS,
  306.                           USER_FAILURE, ASYNC_NONE);
  307.         }
  308.       else
  309.         {/* add char to input buffer */
  310.          Add2Buffer((char)wParam);
  311.         }
  312.       break;
  313.  
  314.     default:
  315.       return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
  316.    }
  317.  return 0;
  318.  
  319. } /* end MainWndProc */